//
//  XLWelcomeViewController.swift
//  WeiBo
//
//  Created by macbook on 16/10/18.
//  Copyright © 2016年 buWangChuXing. All rights reserved.
//

import UIKit
import SDWebImage

class XLWelcomeViewController: UIViewController {
    
    override func loadView() {
        
        view = bgImageView
    }
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        setupUI()
        view.backgroundColor = UIColor.whiteColor()
    }
    
    //背景
    private lazy var bgImageView:UIImageView = UIImageView(imageName: "ad_background")
    
    //头像
    private lazy var iconImage:UIImageView = {
        
        let iamg = UIImageView()
        let img = UIImageView(imageName:"avatar_default_big")
        iamg.layer.borderColor = UIColor.redColor().CGColor
        iamg.layer.borderWidth = 2
        iamg.layer.cornerRadius = 45
        iamg.layer.masksToBounds = true
        
        //设置图片
        iamg.sd_setImageWithURL(NSURL(string:(XLUserAccountTools.shardTools.userAccout?.avatar_large ?? "")), placeholderImage: UIImage(named:"ad_background"))
        
        return iamg
    }()
    
    //文字
    private lazy var contentLabel:UILabel = {
        
        let label = UILabel()
//        label.text = "欢迎回来"
        label.text = XLUserAccountTools.shardTools.userAccout?.screen_name
        label.font = UIFont.systemFontOfSize(20)
        label.textColor = UIColor.grayColor()
        label.textAlignment  = .Center
        label.alpha = 0
        
        return label
    }()
    
    //更改约束
    override func viewDidAppear(animated: Bool) {
        
        super.viewDidAppear(animated)
        
        self.iconImage.snp_updateConstraints { (make) in
            
            make.top.equalTo(self.view).offset(100)
        }
        
        UIView.animateWithDuration(3, delay: 1, usingSpringWithDamping: 0.6, initialSpringVelocity: 0, options: [], animations: {
            
            self.view.layoutIfNeeded()
            
        }) { (_) in
            
            UIView.animateWithDuration(0.25, animations: {
                self.contentLabel.alpha = 1
                
                
                    },completion: { (_) in
                    
                    NSNotificationCenter.defaultCenter().postNotificationName(SwiftRootViewController, object: "welcome")
                    
            })

        }
    }
    
    
    func setupUI()
    {
        view.addSubview(iconImage)
        view.addSubview(contentLabel)
        
        iconImage.snp_makeConstraints { (make) in
            
            make.size.equalTo(CGSizeMake(90, 90))
            make.top.equalTo(view).offset(400)
            make.centerX.equalTo(view)
        }
        
        contentLabel.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(view)
            make.top.equalTo(self.iconImage.snp_bottom).offset(16)
        }
    }


}
